#include "MotionBlurredSprite.h"

MotionBlurredSprite::MotionBlurredSprite(void)
{
}

MotionBlurredSprite::MotionBlurredSprite(BITMAP *_src, BITMAP *_dest, signed int _x, signed int _y, unsigned int _width, unsigned int _height, bool _alphaBlended):
Sprite(_src, _dest, _x, _y, _width, _height, _alphaBlended)
{
	currentDelay = 0;
	setNumberOfTraces(5);
	delay = 3;

	for (unsigned int i = 0; i <= numberOfTraces; ++i)
	{
		lastX[i] = _x;
		lastY[i] = _y;
	}

}


MotionBlurredSprite::~MotionBlurredSprite(void)
{
	if(!Config::LOAD_GRAPHIC)
		release_bitmap(src);
}

void MotionBlurredSprite::draw()
{
	currentDelay ++;
	if(currentDelay > delay)
	{
		for(unsigned int i = 0; i <= (numberOfTraces - 1); i++)
		{
			lastX[numberOfTraces-i] = lastX[numberOfTraces -1 -i];
			lastY[numberOfTraces-i] = lastY[numberOfTraces -i - 1];
		}
		currentDelay = 0;
	}

	lastX[0] = x;
	lastY[0] = y;

	set_trans_blender(0, 0, 0, 255);
	draw_trans_sprite(dest, src, lastX[0], lastY[0]);

	for(unsigned int i = 1; i <= numberOfTraces; i++)
	{
		set_trans_blender(0, 0, 0, 180 - i*30);
		draw_trans_sprite(dest, src, lastX[i], lastY[i]);
	}

	set_alpha_blender();
}

void MotionBlurredSprite::setNumberOfTraces(unsigned int n)
{
	numberOfTraces = n - 1;
	lastX.resize(n);
	lastY.resize(n);
}
